package meijuli.com.join51.business.orderdetail

import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import kotlinx.android.synthetic.main.fragment_order_detail.*
import meijuli.com.join51.business.R
import meijuli.com.join51.business.fragment.BaseFragment
import meijuli.com.join51.business.model.Order
import java.text.SimpleDateFormat
import java.util.*

/**
 * 订单详情页面
 *
 * @author Jack 2018-01-24 14:20
 */
class OrderDetailFramge : BaseFragment() {

    companion object {
        val KEY_ORDER = "order"
    }


    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_order_detail, container, false)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        val order = arguments!![KEY_ORDER] as Order
        if(order.orderStatus == Order.STATUS_WAIT_PAYMENT) {
            layout_order_paytime.visibility = View.GONE
            layout_discount_price.visibility = View.GONE
            layout_actual_price.visibility = View.GONE
            order_status.text = "未收款"
        } else {
            order_status.text = "已收款"
        }

        try {
            setData(layout_order_type, "订单类型：", order.orderTypeDesc)
            setData(layout_order_num, "订单编号：", order.dmId)
            val simpleDate = SimpleDateFormat("yyyy-MM-dd HH:mm")
            val date = Date(order.crtime)
            val time = simpleDate.format(date)
            setData(layout_order_crtime, "创建时间：", time)
            if(order.orderStatus == Order.STATUS_SUCCESS) {
                val dataForm = SimpleDateFormat("yyyy-MM-dd HH:mm")
                val dat = Date(order.crtime)
                val t = dataForm.format(dat)
                setData(layout_order_paytime, "付款时间：", t)
            }
            setData(layout_order_user, "付款人：", order.userNick)
            setData(layout_total_amount, "消费总额", "￥${order.orderPrice}")
            if(order.orderStatus == Order.STATUS_SUCCESS) {
                setData(layout_discount_price, "让利金额", "￥${order.discountPrice}")
                setData(layout_actual_price, "实际到账货款", "￥${order.actPrice}")
            }
        } catch (e: Exception) {

        }

    }

    private fun setData(view: View, name: String?, value: String?) {
        if(TextUtils.isEmpty(value)) {
            view.visibility = View.GONE
            return
        }
        val nameTxt   = view.findViewById<TextView>(R.id.txt_name)
        val valueTxt  = view.findViewById<TextView>(R.id.txt_value)
        nameTxt.text  = name
        valueTxt.text = value
    }
}